package com.commodity.utils;

public class PaginateInfo {
    private final Integer pageNo;//页码

    private final Integer pageSize;//页面大小
    private int total;//总记录数
    private int pages;//总页数

    private int navigateFirst;//导航页起始页
    private int navigateLast;   //导航页最后一页

    private int navigatePages = 5;  //导航页总页数

    public PaginateInfo(Integer pageNo, Integer pageSize) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public int getOffset() {
        return (this.pageNo - 1) * this.pageSize;
    }

    public int getLimit() {
        return this.pageSize;
    }

    public void setTotal(int total) {
        this.total = total;

        //计算总页数
        this.pages = this.total / this.pageSize;
        if (this.total % this.pageSize != 0) {
            this.pages++;
        }

        int half = this.navigatePages / 2;
        this.navigateFirst = this.pageNo - half;
        if (this.navigateFirst < 1) {
            this.navigateFirst = 1;
        }

        this.navigateLast = this.navigateFirst + navigatePages - 1;
        if (this.navigateLast > this.pages) {
            this.navigateLast = this.pages;
            //推导起始页
            this.navigateFirst = this.navigateLast - this.navigatePages + 1;
            if (this.navigateFirst < 1) {
                this.navigateFirst = 1;
            }
        }
    }

    public int getTotal() {
        return total;
    }

    public int getPages() {
        return pages;
    }

    public int getNavigateFirst() {
        return navigateFirst;
    }

    public int getNavigateLast() {
        return navigateLast;
    }

}
